package com.bianguo.android.tencentpushdemo.view.fragment;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.adapters.HomeAdapter;
import com.bianguo.android.tencentpushdemo.events.BaseEvent;
import com.bianguo.android.tencentpushdemo.utils.MyLog;
import com.bianguo.android.tencentpushdemo.utils.PermissionHelper;
import com.bianguo.android.tencentpushdemo.view.activity.DesignActivity;
import com.bianguo.android.tencentpushdemo.view.activity.EditRaidersActivity;
import com.bianguo.android.tencentpushdemo.view.activity.MainActivity;
import com.bianguo.android.tencentpushdemo.view.activity.NewsActivity;
import com.bianguo.android.tencentpushdemo.view.activity.NewsDetailActivity;
import com.bianguo.android.tencentpushdemo.view.activity.RecordActivity;
import com.bianguo.android.tencentpushdemo.view.activity.RecordVideoActivity;
import com.bianguo.android.tencentpushdemo.view.activity.RecyclerPageActivity;
import com.bianguo.android.tencentpushdemo.view.activity.TestActivity;
import com.bianguo.android.tencentpushdemo.view.activity.TopStoryActivity;
import com.bianguo.android.tencentpushdemo.view.activity.TypefaceActivity;
import com.bianguo.android.tencentpushdemo.view.activity.VLayoutActivity;
import com.bianguo.android.tencentpushdemo.view.activity.WeatherActivity;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.itemTouchHelper.SimpleItemTouchHelperCallback;
import com.marshalchen.ultimaterecyclerview.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.scwang.smartrefresh.header.CircleHeader;
import com.scwang.smartrefresh.header.FlyRefreshHeader;
import com.scwang.smartrefresh.header.FunGameBattleCityHeader;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.header.PhoenixHeader;
import com.scwang.smartrefresh.header.TaurusHeader;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.header.fungame.FunGameHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.header.FalsifyHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements HomeAdapter.OnClickItemListener, OnRefreshLoadmoreListener {

    @BindView(R.id.home_fragment_text)
    TextView text;
    @BindView(R.id.home_recycler)
    UltimateRecyclerView homeRecycler;
    @BindView(R.id.home_refresh_layout)
    SmartRefreshLayout refreshLayout;

    private HomeAdapter homeAdapter;
    private List<String> list;
    private MainActivity launcher;

    public HomeFragment() {
    }

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        launcher = (MainActivity) context;
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        text.setText("你好，fragment");
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        homeRecycler.setLayoutManager(layoutManager);
        list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            if (i < 10) {
                list.add("item 0" + i);
            } else {
                list.add("item " + i);
            }
        }
        homeAdapter = new HomeAdapter(getActivity(), list);
        homeRecycler.setAdapter(homeAdapter);
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.head_home_layout, null);
        homeRecycler.setParallaxHeader(headView);
//        homeRecycler.enableDefaultSwipeRefresh(true);
        //为每个item添加头部的布局   这里运用到的其实就是RecyclerView.ItemDecoration
        //没错  可能一般都是用这个来实现item之间的分隔线的  但是线也是一个view，但这个view够大的时候，就是一个头部了
        StickyRecyclerHeadersDecoration stickyRecyclerHeadersDecoration = new StickyRecyclerHeadersDecoration(homeAdapter);
        homeRecycler.addItemDecoration(stickyRecyclerHeadersDecoration);
        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(homeAdapter){
            //这个方法还有别的方法可以重载  可以控制如滑动删除等功能
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//控制拖动的方向   这里设置了智能上下拖动交换位置
                final int swipeFlags = ItemTouchHelper.LEFT ;//控制滑动删除的方向  这里设置了只能左滑删除
//                final int swipeFlags = ItemTouchHelper.LEFT |ItemTouchHelper.RIGHT;//左右滑删除

                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean isItemViewSwipeEnabled() {
                return super.isItemViewSwipeEnabled();//这里控制开启或关闭item是否可以滑动删除的功能
            }

            @Override
            public boolean isLongPressDragEnabled() {
                return super.isLongPressDragEnabled();//控制长按拖动功能
            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(homeRecycler.mRecyclerView);
        homeRecycler.setOnParallaxScroll(new UltimateRecyclerView.OnParallaxScroll(){

            @Override
            public void onParallaxScroll(float percentage, float offset, View parallax) {
//                int round = Math.round(127 + percentage * 128);
//                launcher.shoToolbar(round);
            }
        });
        homeRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

        homeAdapter.setOnClickItemListener(this);

        refreshLayout.setOnRefreshLoadmoreListener(this);
        refreshLayout.setRefreshHeader(new PhoenixHeader(getActivity()));
    }

    @OnClick({R.id.home_fragment_text})
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.home_fragment_text:
                text.setTextSize(14.0f);
                text.setTextColor(Color.RED);
                break;
        }
    }

    @Override
    public void onEvent(BaseEvent event) {
        super.onEvent(event);
        text.setTextSize(24.0f);
        text.setTextColor(Color.BLUE);
    }

    @Override
    public void onClickItem(View view, int position) {
        MyLog.myLog("position = " + position);
        if (0 == position) {
            startActivity(new Intent(getActivity(), WeatherActivity.class));
        } else if (1 == position) {
            startActivity(new Intent(getActivity(), VLayoutActivity.class));
        } else if (2 == position) {
            startActivity(new Intent(getActivity(), TopStoryActivity.class));
        } else if (3 == position) {
            startActivity(new Intent(getActivity(), NewsDetailActivity.class));
        } else if (4 == position) {
            startActivity(new Intent(getActivity(), RecordActivity.class));
        } else if (5 == position) {
            startActivity(new Intent(getActivity(), RecyclerPageActivity.class));
        } else {
            startActivity(new Intent(getActivity(), DesignActivity.class));
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshLayout.finishLoadmore(5000);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshLayout.finishRefresh(5000);
    }
}
